"use strict";(function(){if(!window.BX){window.BX={}}else if(window.BX.RestClient){return}var e=window.BX;e.RestClient=function(e){e=e||{};this.endpoint=e.endpoint||"/rest";this.queryParams=e.queryParams||""};e.RestClient.prototype.callMethod=function(e,t,r,i,s){return n({method:e,data:t,callback:r,sendCallback:i,logTag:s,endpoint:this.endpoint,queryParams:this.queryParams})};e.RestClient.prototype.callBatch=function(e,t,i,s,o){var a=r.isArray(e)?[]:{};var u=0;var l=function(e){n.batch(e,t,i,s,this.endpoint,this.queryParams,o)}.bind(this);for(var f in e){var c=null,p=null;if(!!e[f]&&e.hasOwnProperty(f)){if(r.isArray(e[f])){c=e[f][0];p=e[f][1]}else if(!!e[f].method){c=e[f].method;p=e[f].params}if(!!c){u++;a[f]=[c,p]}}}if(u>0){var d=function(e){return function(t){a[e]=a[e][0]+"?"+t;if(--u<=0)l(a)}};for(var h in a){if(a.hasOwnProperty(h)){n.prepareData(a[h][1],"",d(h))}}}};e.RestClient.prototype.setEndpoint=function(e){this.endpoint=e};e.RestClient.prototype.setQueryParams=function(e){this.queryParams=e};if(typeof e.namespace!=="undefined"){var t=new e.RestClient;if(typeof e.rest=="undefined"){e.rest={}}e.rest.callMethod=function(e,r,n,i,s){return t.callMethod(e,r,n,i,s)};e.rest.callBatch=function(e,r,n,i,s){return t.callBatch(e,r,n,i,s)}}var r={isArray:function(e){return e&&Object.prototype.toString.call(e)=="[object Array]"},isFunction:function(e){return e===null?false:typeof e=="function"||e instanceof Function},isString:function(e){return e===""?true:e?typeof e=="string"||e instanceof String:false},isDomNode:function(e){return e&&typeof e=="object"&&"nodeType"in e},isDate:function(e){return e&&Object.prototype.toString.call(e)=="[object Date]"},buildQueryString:function(e){var t="";for(var r in e){if(!e.hasOwnProperty(r)){continue}var n=e[r];if(this.isArray(n)){n.forEach(function(e,n){t+=encodeURIComponent(r+"["+n+"]")+"="+encodeURIComponent(e)+"&"})}else{t+=encodeURIComponent(r)+"="+encodeURIComponent(n)+"&"}}if(t.length>0){t=t.substr(0,t.length-1)}return t},clone:function(e,t){var r,n,i;if(t!==false)t=true;if(e===null)return null;if(this.isDomNode(e)){r=e.cloneNode(t)}else if(typeof e=="object"){if(this.isArray(e)){r=[];for(n=0,i=e.length;n<i;n++){if(typeof e[n]=="object"&&t)r[n]=this.clone(e[n],t);else r[n]=e[n]}}else{r={};if(e.constructor){if(this.isDate(e))r=new Date(e);else r=new e.constructor}for(n in e){if(typeof e[n]=="object"&&t)r[n]=this.clone(e[n],t);else r[n]=e[n]}}}else{r=e}return r}};var n=function(t){var s=!!t.callback&&r.isFunction(t.callback);var o=typeof e.Promise==="undefined"||s?null:new e.Promise;var a=t.sendCallback||function(){};var u=t.withoutRestoringCsrf||false;var l=n.xhr();var f=t.endpoint+"/"+n.escape(t.method)+".json"+(t.logTag?"?logTag="+t.logTag:"");l.open("POST",f);l.setRequestHeader("Content-Type","application/x-www-form-urlencoded");var c=false;l.onprogress=function(){};l.ontimeout=function(){};l.timeout=0;l.onload=function(){if(c)return;l.onload=function(){};var r=n.isSuccess(l);var a=l.status;if(r){var f=l.responseText;if(f.length>0){try{f=JSON.parse(f)}catch(e){r=false}}if(a==401){if(f.sessid&&!u){e.message({bitrix_sessid:f.sessid});console.warn("BX.rest: your csrf-token has expired, send query with a new token");t.withoutRestoringCsrf=true;if(!s){t.callback=function(e){if(e.error()){o.reject(e)}else{o.fulfill(e)}}}n(t);return true}}else if(a==0){f={result:{},error:"ERROR_NETWORK",error_description:"A network error occurred while the request was being executed."}}else{if(a==200){if(f.length<=0){f={result:{}}}}else if(f.length<=0){f={result:{},error:"BLANK_ANSWER_WITH_ERROR_CODE",error_description:"Blank answer with error http code: "+a}}}}l=null;if(r){var p=new i(f,t,a);if(s){t.callback.apply(window,[p])}else{if(p.error()){o.reject(p)}else{o.fulfill(p)}}}else{var p=new i({error:"ERROR_UNEXPECTED_ANSWER",error_description:"Server returned an unexpected response.",ex:{}},t,0);if(s){t.callback.apply(window,[p])}else{o.reject(p)}}};l.onerror=function(e){var r=new i({error:"ERROR_NETWORK",error_description:"A network error occurred while the request was being executed.",ex:e},t,0);if(s){t.callback.apply(window,[r])}else{o.reject(r)}};var p="";if(t.queryParams){p=r.buildQueryString(t.queryParams)}else if(typeof e.bitrix_sessid!=="undefined"){p="sessid="+e.bitrix_sessid()}if(typeof t.start!=="undefined"){p+="&start="+parseInt(t.start)}if(!!t.data){n.prepareData(t.data,"",function(e){p+="&"+e;l.send(p);a(l)})}else{l.send(p);a(l)}return s||!o?l:o};n.batch=function(e,t,s,o,a,u,l){return n({method:"batch",data:{halt:!!s?1:0,cmd:e},callback:function(n,s,o){if(!!t){var l=n.error();var f=n.data();var c=r.isArray(e)?[]:{};for(var p in e){if(!!e[p]&&e.hasOwnProperty(p)){if(r.isString(e[p])){var d=e[p].split("?")}else{d=[r.isArray(e[p])?e[p][0]:e[p].method,r.isArray(e[p])?e[p][1]:e[p].data]}if(f&&(typeof f.result[p]!=="undefined"||typeof f.result_error[p]!=="undefined")){c[p]=new i({result:typeof f.result[p]!=="undefined"?f.result[p]:{},error:f.result_error[p]||undefined,total:f.result_total[p],time:f.result_time[p],next:f.result_next[p]},{method:d[0],data:d[1],callback:t,endpoint:a,queryParams:u},n.status)}else if(l){c[p]=new i({result:{},error:l.ex,total:0},{method:d[0],data:d[1],callback:t,endpoint:a,queryParams:u},n.status)}}}t.apply(window,[c])}},sendCallback:o,endpoint:a,queryParams:u,logTag:l})};n.xhr=function(){return new XMLHttpRequest};n.escape=function(e){return encodeURIComponent(e)};n.prepareData=function(e,t,i){var s="",a=[];if(r.isString(e)||e===null){i.call(document,e||"")}else{for(var u in e){if(!e.hasOwnProperty(u)){continue}var l=n.escape(u);if(t)l=t+"["+l+"]";if(typeof e[u]==="object"){a.push([l,e[u]])}else{if(s.length>0){s+="&"}if(typeof e[u]==="boolean"){s+=l+"="+(e[u]?1:0)}else{s+=l+"="+n.escape(e[u])}}}var f=a.length;if(f>0){var c=function(e){s+=(!!e?"&":"")+e;if(--f<=0){i.call(document,s)}};var p=f;for(var u=0;u<p;u++){if(r.isDomNode(a[u][1])){if(a[u][1].tagName.toUpperCase()==="INPUT"&&a[u][1].type==="file"){if(o.canUse()){o(a[u][1],function(e){return function(t){if(r.isArray(t)&&t.length>0){c(e+"[0]="+n.escape(t[0])+"&"+e+"[1]="+n.escape(t[1]))}else{c(e+"=")}}}(a[u][0]))}}else if(typeof a[u][1].value!=="undefined"){c(a[u][0]+"="+n.escape(a[u][1].value))}else{c("")}}else if(r.isDate(a[u][1])){c(a[u][0]+"="+n.escape(a[u][1].toJSON()))}else if(r.isArray(a[u][1])&&a[u][1].length<=0){c(a[u][0]+"=")}else{n.prepareData(a[u][1],a[u][0],c)}}}else{i.call(document,s)}}};n.isSuccess=function(e){return typeof e.status==="undefined"||e.status>=200&&e.status<300||e.status===304||e.status>=400&&e.status<500||e.status===1223||e.status===0};var i=function(e,t,n){this.answer=e;this.query=r.clone(t);this.status=n;if(typeof this.answer.next!=="undefined"){this.answer.next=parseInt(this.answer.next)}if(typeof this.answer.error!=="undefined"){this.answer.ex=new s(this.status,typeof this.answer.error==="string"?this.answer:this.answer.error)}};i.prototype.data=function(){return this.answer.result};i.prototype.time=function(){return this.answer.time};i.prototype.error=function(){return this.answer.ex};i.prototype.error_description=function(){return this.answer.error_description};i.prototype.more=function(){return!isNaN(this.answer.next)};i.prototype.total=function(){return parseInt(this.answer.total)};i.prototype.next=function(e){if(this.more()){this.query.start=this.answer.next;if(!!e&&r.isFunction(e)){this.query.callback=e}return n(this.query)}return false};var s=function(e,t){this.status=e;this.ex=t};s.prototype.getError=function(){return this.ex};s.prototype.getStatus=function(){return this.status};s.prototype.toString=function(){return this.ex.error+(!!this.ex.error_description?": "+this.ex.error_description:"")+" ("+this.status+")"};var o=function(e,t){if(o.canUse()){var r=e.files,n=0,i=e.multiple?[]:null;for(var s=0,a;a=r[s];s++){var u=new window.FileReader;u.BXFILENAME=r[s].name;u.onload=function(e){e=e||window.event;var r=[this.BXFILENAME,btoa(e.target.result)];if(i===null)i=r;else i.push(r);if(--n<=0){t(i)}};u.readAsBinaryString(a)}n=s;if(n<=0){t(i)}}};o.canUse=function(){return!!window.FileReader}})();